<!DOCTYPE html>
<html>
<head><@lib.adminhead/> <@lib.adminfootjs/>
</head>
<body>

	<div id="wrapper">
		<!-- Page Content -->
		<div class="container-fluid">
			<div class="row">
				<div class="col-lg-12">
					<h1 class="page-header">定时任务列表</h1>

				</div>
			</div>
			<div class="row">
				<div class="col-lg-12">
					<div class="panel panel-default">
						<div class="panel-heading">
							定时任务列表
							<div class="pull-right">
								<div class="btn-group">
									<button class="pull-right btn btn-primary btn-xs" id="add">增加</button>
								</div>
							</div>
						</div>
						<!-- /.panel-heading -->
						<div class="panel-body">
							<div class="table-responsive">
								<table class="table table-striped table-bordered table-hover">
									<thead>
										<tr>
											<th>#</th>
											<th>任务名</th>
											<th>任务组</th>
											<th>cron表达式</th>
											<th>状态</th>
											<th>任务类</th>
											<th>上次执行</th>
											<th>下次执行</th>
											<th>描述</th>
											<th>操作</th>
										</tr>
									</thead>
									<tbody>
										<#if scheduleJobs!> <#list scheduleJobs as job>
										<tr>
											<td>${job_index}</td>
											<td>${job.name}</td>
											<td>${job.group}</td>
											<td><button type="button" class="btn  btn-outline btn-success updateobj" data-name="${job.name}"
													data-group="${job.group}" data-cronexpression="${job.cronExpression}">${job.cronExpression}</button></td>
											<td><#if job.status=='NORMAL'> <a type="button" class="btn btn-success updateStatus"
												data-tostatus="stop" data-name="${job.name}" data-group="${job.group}" href="javascript:;">${job.status}</a>
												<#elseif job.status=='PAUSED'> <a type="button" class="btn btn-warning updateStatus" data-tostatus="resume"
												data-name="${job.name}" data-group="${job.group}" href="javascript:;">${job.status}</a> <#else>
												${job.status} </#if>
											</td>
											<td>${job.className}</td>
											<td>${(job.previousFireTime?string('dd.MM.yyyy HH:mm:ss'))!}</td>
											<td>${(job.nextFireTime?string('dd.MM.yyyy HH:mm:ss'))!}</td>
											<td>${job.description}</td>
											<td>
												<button type="button" class="btn btn-danger delobj" data-name="${job.name}" data-group="${job.group}">删除</button>
												<button type="button" class="btn btn-success startNow" data-name="${job.name}" data-group="${job.group}">立即执行</button>

											</td>
										</tr>
										</#list> <#else>
										<tr>
											<td colspan="10">没有记录</td>
										</tr>
										</#if>
									</tbody>
								</table>
							</div>
							<!-- /.table-responsive -->
						</div>
					</div>
				</div>
			</div>
			<!-- /.container-fluid -->
		</div>
		<!-- /#page-wrapper -->

	</div>
	<form method="post" action="${ctx}/admin/task/create" id="addform" style="display: none">
		<div class="panel-body">
			<div class="row">
				<div class="col-xs-12">
					<div class="form-group">
						<label for="username">任务名：</label> 
						<input class="form-control" type="text" id="name" name="name" placeholder="任务名"
							required>
					</div>
					<div class="form-group">
						<label for="password">任务组：</label> <input class="form-control" type="text" id="group" name="group"
							placeholder="任务组" required>
					</div>
					<div class="form-group">
						<label for="repassword">表达式：</label> <input class="form-control" type="text" id="cronExpression"
							name="cronExpression" placeholder="表达式" required>
					</div>
					<div class="form-group">
						<label for="repassword">任务类：</label> <input class="form-control" type="text" name="className" id="className"
							placeholder="任务类" required>
					</div>
					<div class="text-center">
						<input type="submit" class="btn btn-success" value="添加" />
					</div>
				</div>
			</div>
		</div>
	</form>

	<script type="text/javascript">
		$(function() {
			$("#addform").validate(
					{
						errorPlacement : function(error, element) {
							$(element).closest("form").find(
									"label[for='" + element.attr("id") + "']").append(error);
						},
						errorClass:'text-danger',
						errorElement: "span"
					});
			$("#add").click(function() {
				var index = layer.open({
					type : 1,
					title : '添加任务',
					shadeClose : true, //点击遮罩关闭
					content : $("#addform"),
					shade: 0
				});
			});
			$(".delobj").click(function(){
				var name = $(this).data("name");
				var group = $(this).data("group");
				layer.confirm('确认删除此任务?', {
				    btn: ['确定','取消'] 
				}, function(){
				    layer.msg('正在删除中..', {icon: 1});
				    window.location.href="${ctx}/admin/task/delete?name="+name+"&group="+group;
				});
			});
			$(".updateobj").click(function(){
				var button = $(this);
				var name = button.data("name");
				var group = button.data("group");
				var cronexpression = button.data("cronexpression");
				layer.prompt({
					  formType: 0,
					  value: cronexpression,
					  title: '请输入表达式'
					}, function(value, index, elem){
						 button.addClass("disabled");
						 var load = layer.load(1);
						$.post("${ctx}/admin/task/update", {"name":name,"group":group,"cronExpression":value}, function(data){
							if(data){
								button.html(value);
								button.data("cronexpression",value);
								layer.msg('修改成功');
							}else{
								layer.msg('修改失败');
							}
							button.removeClass("disabled");
						});
					  layer.close(load);
					  layer.close(index);
					},"json");
					        
			});
			$(".updateStatus").click(function(){
				var button = $(this);
				var name = button.data("name");
				var group = button.data("group");
				var tostatus=button.data("tostatus");
				 window.location.href="${ctx}/admin/task/updateStatus?name="+name+"&group="+group+"&tostatus="+tostatus;        
			});
			$(".startNow").click(function(){
				var button = $(this);
				var name = button.data("name");
				var group = button.data("group");
				 window.location.href="${ctx}/admin/task/startNow?name="+name+"&group="+group;
			});
			
		});
	</script>
</body>
</html>
